package com.jjb.www.ui.dialog;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;

import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.contrarywind.view.WheelView;
import com.hjq.base.BaseDialog;
import com.jjb.www.R;
import com.jjb.www.aop.SingleClick;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: lkw
 * time:   2021/9/11 10:41
 * des:    完善信息选择对话框
 */
public final class PerfectInfoDialog {

    public static final class Builder
            extends BaseDialog.Builder<Builder> {

        @SuppressWarnings("rawtypes")
        @Nullable
        private OnListener mListener;
        private final WheelView wheelView;
        private final TextView mTvTitle;
        private final AppCompatTextView tvCancel;
        private final AppCompatTextView tvConfirm;
        private List<String> mOptionsItems;

        public Builder(Context context) {
            super(context);
            setContentView(R.layout.education_dialog);
            wheelView = findViewById(R.id.wheel_view);
            tvCancel = findViewById(R.id.tv_cancel);
            tvConfirm = findViewById(R.id.tv_confirm);
            mTvTitle = findViewById(R.id.tv_title);
            setOnClickListener(tvCancel, tvConfirm);
            wheelView.setCyclic(false);
            wheelView.setTextSize(20);
            wheelView.setLineSpacingMultiplier(2f);
            wheelView.setDividerType(WheelView.DividerType.FILL);
            mOptionsItems = new ArrayList<>();
        }

        @SuppressWarnings("rawtypes")
        public Builder setListener(OnListener listener) {
            mListener = listener;
            return this;
        }

        public Builder setTitle(String title) {
            mTvTitle.setText(title);
            return this;
        }

        public Builder setList(List<String> list) {
            mOptionsItems = list;
            wheelView.setAdapter(new ArrayWheelAdapter<>(list));
            return this;
        }

        @SuppressWarnings("all")
        @SingleClick
        @Override
        public void onClick(View view) {
            if (view == tvCancel) {
                dismiss();
                if (mListener == null) {
                    return;
                }
                mListener.onCancel(getDialog());
            } else if (view == tvConfirm) {
                dismiss();
                if (mListener == null) {
                    return;
                }
                mListener.onCompleted(getDialog(), mOptionsItems.get(wheelView.getCurrentItem()),wheelView.getCurrentItem());
            }
        }
    }

    public interface OnListener<T> {

        /**
         * 选择完成时回调
         *
         * @param t 单选选项对象
         */
        void onCompleted(BaseDialog dialog, T t,int position);

        /**
         * 点击取消时回调
         */
        default void onCancel(BaseDialog dialog) {
        }
    }
}
